from django.db import models

# Create your models here.

from django.db import models

class Order(models.Model):
    # 主键字段
    id = models.BigAutoField(primary_key=True, verbose_name="订单ID")

    # 用户和商品信息
    user_id = models.BigIntegerField(verbose_name="用户ID")
    goods_id = models.BigIntegerField(verbose_name="商品ID")
    delivery_addr_id = models.BigIntegerField(verbose_name="收货地址ID")

    # 商品详情
    goods_name = models.CharField(max_length=16, blank=True, null=True, verbose_name="商品名称")
    goods_count = models.IntegerField(verbose_name="商品数量")
    goods_price = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="商品价格")

    # 订单信息
    order_channel = models.SmallIntegerField(choices=[(1, "秒杀"), (2, "普通")], default=1, verbose_name="订单渠道")
    status = models.SmallIntegerField(
        choices=[
            (0, "待支付"),
            (1, "已支付"),
            (2, "已发货"),
            (3, "已完成"),
            (4, "已取消"),
        ],
        default=0,
        verbose_name="订单状态"
    )

    # 时间字段
    create_date = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    pay_date = models.DateTimeField(blank=True, null=True, verbose_name="支付时间")

    class Meta:
        db_table = 'order'  # 指定数据库表名
        verbose_name = "订单"
        verbose_name_plural = "订单列表"

    def __str__(self):
        return f"订单ID: {self.id}, 用户ID: {self.user_id}, 商品名称: {self.goods_name}"


class SeckillOrder(models.Model):
    id = models.BigAutoField(primary_key=True, verbose_name="秒杀订单ID")
    user_id = models.BigIntegerField(verbose_name="用户ID")
    goods_id = models.BigIntegerField(verbose_name="商品ID")
    order = models.ForeignKey(
        Order,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="seckill_orders",
        verbose_name="关联的普通订单"
    )

    class Meta:
        db_table = 't_seckill_order'
        verbose_name = "秒杀订单"
        verbose_name_plural = "秒杀订单列表"

    def __str__(self):
        return f"秒杀订单ID: {self.id}, 用户ID: {self.user_id}, 商品ID: {self.goods_id}"